package com.llly.controller;

import com.llly.client.OrderFeignClient;
import com.llly.pojo.Easybuy_order;
import com.llly.pojo.Easybuy_user;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {
    @Autowired
    private OrderFeignClient orderFeignClient;

    //根据用户id查询订单
    @RequestMapping("OrderShowAll")
    public ModelAndView OrderList(ModelAndView mv, Integer userId, Integer PageIndex, HttpSession session){
        if (PageIndex==null){
            PageIndex=1;
        }
        Easybuy_user user = (Easybuy_user)session.getAttribute("user");
        userId=user.getId();
        Integer type = user.getType();
        Map<String, Object> stringObjectMap = orderFeignClient.ShowOrder(userId, PageIndex);
        List<Easybuy_order> pg = (List<Easybuy_order>) stringObjectMap.get("pg");
        mv.addObject("Odr",pg);
        System.out.printf("pg++++++++++++++++++++"+pg);
        mv.addObject("Total",stringObjectMap.get("Total"));
        mv.addObject("PageNum",stringObjectMap.get("PageNum"));
        mv.addObject("Pages",stringObjectMap.get("Pages"));
        Integer pages = (Integer) stringObjectMap.get("Pages");
        List pag=new ArrayList();
        for (int i=1; i<=pages; i++){
            pag.add(i);
       }
        mv.addObject("listPage",pag);
        mv.setViewName("Manager/backstage_order");

        return mv;
    }
    //删除订单
    @RequestMapping("delOrder")
    public ModelAndView delOrder(String serialNumber,ModelAndView mv) {
        int i = orderFeignClient.delOrder(serialNumber);

        mv.setViewName("forward:/OrderShowAll");
        return mv;
    }
    //删除全部订单
    @RequestMapping("delAllOrder")
    public ModelAndView delAllOrder(ModelAndView mv,String loginName){
        System.out.printf("loginName"+loginName);
        int a = orderFeignClient.delAllOrder(loginName);
        mv.setViewName("forward:/OrderShowAll");
        return mv;
    }
    //管理页面，查询全部订单
    @RequestMapping("queryorder")
    public ModelAndView queryorder(ModelAndView mv,Integer PageIndex,HttpSession session){
        if (PageIndex==null){
            PageIndex=1;
        }
        Map<String, Object> stringObjectMap = orderFeignClient.ShowOrder(null,PageIndex);
        List<Easybuy_order> pg = (List<Easybuy_order>) stringObjectMap.get("pg");
        mv.addObject("Odr",pg);
        mv.addObject("Total",stringObjectMap.get("Total"));
        mv.addObject("PageNum",stringObjectMap.get("PageNum"));
        mv.addObject("Pages",stringObjectMap.get("Pages"));
        Integer pages = (Integer) stringObjectMap.get("Pages");
        List pag=new ArrayList();
        for (int i=1; i<=pages; i++){
            pag.add(i);
        }
        mv.addObject("listPage",pag);
        mv.setViewName("Manager/backstage_Allorder");
        return mv;
    }

}
